package com.lxt.controller;

import com.lxt.common.HttpResult;
import com.lxt.common.PagedGridResult;
import com.lxt.service.ItemsESService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author kevin.lv
 * @date 2023/2/21 20:40
 */
@RestController
@RequestMapping("items")
public class ItemsController {


    @Autowired
    private ItemsESService itemsESService;

    @GetMapping("/hello")
    public Object hello() {
        return "hello ElasticSearch";
    }

    @GetMapping("/es/search")
    public HttpResult searchItems(
            String keywords,
            String sort,
            Integer page,
            Integer pageSize) {

        if (StringUtils.isBlank(keywords)) {
            return HttpResult.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 20;
        }

        page--;

        PagedGridResult result = itemsESService.searchItems(keywords, sort, page, pageSize);
        return HttpResult.ok(result);
    }


}
